package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.service.AppointmentService;
import cn.wolfcode.business.util.DateUtil;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Override
    public List<BusAppointment> selectAppointmentList(BusAppointment appointment) {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    @Override
    public BusAppointment selectAppointmentById(Long id) {
        return appointmentMapper.selectAppointmentById(id);
    }

    @Override
    public int save(BusAppointment appointment) {
        if (appointment==null){
            throw new RuntimeException("参数异常");
        }
        if(appointment.getCustomerName()==null||appointment.getCustomerPhone()==null
                ||appointment.getAppointmentTime()==null||appointment.getLicensePlate()==null
                ||appointment.getCarSeries()==null||appointment.getServiceType()==null){
            throw new RuntimeException("参数异常");
        }
        if (!RegexUtils.isPhoneLegal(appointment.getCustomerPhone())){
            throw new RuntimeException("手机号不合法");
        }
        if ((VehiclePlateNoUtil.getVehiclePlateNo(appointment.getLicensePlate())==null)){
            throw new RuntimeException("车牌号不合法");
        }
        Date update = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!appointment.getAppointmentTime().after(update)) {
            throw new RuntimeException("预约时间不能小于当前时间");
        }
        appointment.setStatus(BusAppointment.STATUS_BOOKING);
        appointment.setCreateTime(new Date());
        return appointmentMapper.save(appointment);
    }

    @Override
    public int update(BusAppointment appointment) {
        if (appointment==null){
            throw new RuntimeException("参数异常");
        }
        if(appointment.getCustomerName()==null||appointment.getCustomerPhone()==null
                ||appointment.getAppointmentTime()==null||appointment.getLicensePlate()==null
                ||appointment.getCarSeries()==null||appointment.getServiceType()==null){
            throw new RuntimeException("参数异常");
        }
        if (!RegexUtils.isPhoneLegal(appointment.getCustomerPhone())){
            throw new RuntimeException("手机号不合法");
        }
        if ((VehiclePlateNoUtil.getVehiclePlateNo(appointment.getLicensePlate())==null)){
            throw new RuntimeException("车牌号不合法");
        }
        Date update = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!appointment.getAppointmentTime().after(update)) {
            throw new RuntimeException("预约时间不能小于当前时间");
        }

        if (!BusAppointment.STATUS_BOOKING.equals(appointment.getStatus())){
            throw new RuntimeException("只有预约中才能进行编辑操作");
        }
        return appointmentMapper.update(appointment);
    }

    @Override
    public int deleteAppointmentByIds(Long[] ids) {
        Long id = ids[0];
        if (id==null){
            throw new RuntimeException("参数异常");
        }
        BusAppointment appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment==null){
            throw new RuntimeException("参数异常");
        }
        if(!BusAppointment.STATUS_BOOKING.equals(appointment.getStatus())){
            throw new RuntimeException("只有预约中才能进行删除操作");
        }
        return appointmentMapper.deleteAppointmentByIds(ids);
    }

    @Override
    public int arrival(Long id) {
        if (id==null){
            throw new RuntimeException("参数异常");
        }
        BusAppointment appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment==null){
            throw new RuntimeException("参数异常");
        }
        if(!BusAppointment.STATUS_BOOKING.equals(appointment.getStatus())){
            throw new RuntimeException("只有预约中才能进行到店操作");
        }
        return appointmentMapper.arrival(new Date(),BusAppointment.STATUS_ARRIVED,id);
    }

    @Override
    public int cancel(Long id) {
        if (id==null){
            throw new RuntimeException("参数异常");
        }
        BusAppointment appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment==null){
            throw new RuntimeException("参数异常");
        }
        if(!BusAppointment.STATUS_BOOKING.equals(appointment.getStatus())){
            throw new RuntimeException("只有预约中才能进行取消操作");
        }
        return appointmentMapper.cancel(BusAppointment.STATUS_USER_CANCELLED,id);
    }

    @Override
    public void updateStatus(Long appointmentId, Integer statusPaid) {
        appointmentMapper.cancel(statusPaid,appointmentId);
    }


}
